<?php

namespace app\cash\logic\printer;

use app\beautiful\model\appointment\AppointmentModel;

use app\beautiful\model\logs\ProjectWriteOffModel;

use app\beautiful\model\logs\StoreWriteOffModel;

use app\beautiful\model\mall\FreightTemplateModel;
use app\beautiful\model\member\MemberModel;

use app\beautiful\model\member\MemberOnceCardItemModel;

use app\beautiful\model\member\MemberOnceCardModel;

use app\beautiful\model\member\MemberSnapUpModel;

use app\beautiful\model\order\BuyOrderModel;

use app\beautiful\model\order\CashierOrderItemModel;

use app\beautiful\model\order\CashierOrderModel;

use app\beautiful\model\order\GoodsOrderItemModel;

use app\beautiful\model\order\GoodsOrderModel;

use app\beautiful\model\order\StoreOrderModel;
use app\cash\model\OpenTableModel;
use app\common\model\account\AccountModel;
use app\common\model\account\ShopAccountModel;


class PrinterYlyContentLogic
{

    protected $order;

    protected $member;

    protected $order_item;

    protected $order_goods_num;

    protected $record;

    protected $once_card;

    //预结单
    public function advanceOrder($shop,$id, $goods_id, $print_size = 80,$account_id=0)
    {

        {
            $this->checkGoodsOrder($id);

            $this->chechGoodsOrderItem($id, $goods_id);

            if ($this->order->member_id) {

                $this->checkMember($this->order->member_id);

            }
            //
            if($this->order->service_type==1){
                $service_type = '堂食';
            }
            if($this->order->service_type==2){
                $service_type = '打包';
            }
            if($this->order->service_type==3){
                $service_type = '外卖';
            }
            //开台信息
            if($this->order->open_table_id){

                $ot = OpenTableModel::getById(['open_table_id'=>$this->order->open_table_id,'is_delete'=>0],'o.*,t.name');

            }

            $member_name = $this->member?$this->member->name:'散客';


            //操作人
            $account = AccountModel::where(['account_id'=>$account_id,'status'=>1,'is_delete'=>0])->find();

            $operate_name = $account?$account->username:'';

            $content = "";

            $content .= "<FS><center>{shop_name}</center></FS>\n";

            $content .= "<FW><center>预结单-{service_type}-未结</center></FW>";

            $content .= "单号:{order_sn}";

            if(isset($ot) && $ot) $content .= "          人数:{dinner_num}";

            $content .= "          反结:1\n";

            $content .= "打印次数:1          预结:1";

            if(isset($ot) && $ot) $content .= "          台号:{table_name}";

            $content .= "\n";

            $content .= "--------------------------------";

            if ($print_size == 80) $content .= "----------------";

            $content .= "\n";

            $content .= '<table><tr><td>品名</td><td>数量</td><td>单价</td><td>小计</td></tr>';

            $content .= "--------------------------------";

            if ($print_size == 80) $content .= "----------------";

            $content .= "\n";

            foreach ($this->order_item as $item) {
                $content .= '<tr>';
                $price = bcmul((string)$item['goods_num'], (string)$item['goods_price'], 2);
                $name = $item['goods_sku_name'] ? $item['goods_name'] . '(' . $item['goods_sku_name'] . ')' : $item['goods_name'];
                $content .= "<td>{$name}</td><td>{$item['goods_num']}</td><td>{$item['goods_price']}</td><td>{$price}</td>";
                $content .= '</tr>';
                unset($price);
            }
            $content .= '</table>';

            $content .= "--------------------------------";

            if ($print_size == 80) $content .= "----------------";

            $content .= "\n";

            $content .= "消费合计:{total_price}\n";

            $content .= "应收:{need_pay}\n";

            $content .= "--------------------------------";

            if ($print_size == 80) $content .= "----------------";

            $content .= "\n";

            $content .= "会员:{member_name}\n";

            $content .= "--------------------------------";

            if ($print_size == 80) $content .= "----------------";

            $content .= "操作人:{operate_name}    收银台点餐\n";

            $content .= "账单:{order_sn}\n";

            $content .= "打印时间:{date}\n";

            $content .= "--------------------------------";

            if ($print_size == 80) $content .= "----------------";

            $content .= "系统由汇飓客提供，服务热销400400400\n\n\n";


            $content = str_replace('{shop_name}', $shop->name, $content);

            $content = str_replace('{service_type}', $service_type, $content);

            $content = str_replace('{order_sn}', $this->order->order_sn, $content);

            $content = str_replace('{dinner_num}', $ot->dinner_num, $content);

            $content = str_replace('{table_name}', $ot->name, $content);

            $content = str_replace('{total_price}', $this->order->total_price, $content);

            $content = str_replace('{need_pay}', $this->order->need_pay, $content);

            $content = str_replace('{member_name}', $member_name, $content);

            $content = str_replace('{operate_name}', $operate_name, $content);

            $content = str_replace('{date}', date('Y-m-d H:i:s'), $content);


            return $content;
        }


    }



    protected function checkOrder($id)

    {

        if (null === $order = BuyOrderModel::find($id)) $this->error('订单不存在');

        $this->order = $order;

    }


    protected function checkGoodsOrder($id)
    {

        if (null === $order = GoodsOrderModel::find($id)) $this->error('订单不存在');

        $this->order = $order;

    }


    protected function chechGoodsOrderItem($id, $goods_id = 0)

    {

        $order_item = GoodsOrderItemModel::where('order_id', $id)->whereIn('goods_id', $goods_id)->select()->toArray();

        $order_goods_num = GoodsOrderItemModel::where('order_id', $id)->whereIn('goods_id', $goods_id)->sum('goods_num');

        $this->order_goods_num = $order_goods_num;

        $this->order_item = $order_item;

    }


    protected function checkStoreGoodsRecord($id)

    {

        if (null === $this->record = StoreWriteOffModel::find($id)) $this->error('记录不存在');

    }


    protected function checkStoreGoodsOrder($id)

    {

        if (null === $this->order = StoreOrderModel::find($id)) $this->error('订单不存在');

    }


    protected function checkMember($id)

    {

        if (null === $member = MemberModel::find($id)) $this->error('找不到会员');

        $this->member = $member;

    }


    protected function checkProjectRecord($id)

    {

        if (null === $this->record = ProjectWriteOffModel::find($id)) $this->error('记录不存在');

    }


    protected function checkMemberSnapUp($id)

    {

        if (null === $this->order = MemberSnapUpModel::find($id)) $this->error('订单不存在');

    }


    protected function checkMemberOnceCardItem($id)

    {

        if (null === $this->order = MemberOnceCardItemModel::find($id)) $this->error('订单不存在');

    }


    protected function checkMemberOnceCard($id)

    {

        if (null === $this->once_card = MemberOnceCardModel::find($id)) $this->error('订单不存在');

    }


    public function checkAppointment($id)

    {

        if (null === $this->order = AppointmentModel::find($id)) $this->error('预约不存在');

    }


    public function checkCashOrder($id)

    {

        if (null === $this->order = CashierOrderModel::find($id)) $this->error('订单不存在');

    }


    public function checkCashOrderItem($id)

    {

        $order_item = CashierOrderItemModel::where('order_id', $id)->select()->toArray();

        $this->order_item = $order_item;

    }


    public function getArrContent($arr, $A, $C, $D)

    {

        $orderInfo = '';

        $orderInfo .= "产品                 数量 金额\n";

        $orderInfo .= "--------------------------------\n";

        foreach ($arr as $k5 => $v5) {

            $name = $v5['goods_name'];

            if ($v5['goods_sku_id'] > 0) {

                $name = $v5['goods_name'] . '(' . $v5['goods_sku_name'] . ')';

            }

            //$price = $v5['goods_sku_name'];

            $num = $v5['goods_num'];

            $prices = $v5['goods_price'];

            $kw3 = '';

            $kw1 = '';

            $kw2 = '';

            $kw4 = '';

            $str = $name;

            $blankNum = $A;//名称控制为14个字节

            $lan = mb_strlen($str, 'utf-8');

            $m = 0;

            $j = 1;

            $blankNum++;

            $result = array();

//            if(strlen($price) < $B){

//                $k1 = $B - strlen($price);

//                for($q=0;$q<$k1;$q++){

//                    $kw1 .= ' ';

//                }

//                $price = $price.$kw1;

//            }

            if (strlen($num) < $C) {

                $k2 = $C - strlen($num);

                for ($q = 0; $q < $k2; $q++) {

                    $kw2 .= ' ';

                }

                $num = $num . $kw2;

            }

            if (strlen($prices) < $D) {

                $k3 = $D - strlen($prices);

                for ($q = 0; $q < $k3; $q++) {

                    $kw4 .= ' ';

                }

                $prices = $prices . $kw4;

            }

            for ($i = 0; $i < $lan; $i++) {

                $new = mb_substr($str, $m, $j, 'utf-8');

                $j++;

                if (mb_strwidth($new, 'utf-8') < $blankNum) {

                    if ($m + $j > $lan) {

                        $m = $m + $j;

                        $tail = $new;

                        $lenght = iconv("UTF-8", "GBK//IGNORE", $new);

                        $k = $A - strlen($lenght);

                        for ($q = 0; $q < $k; $q++) {

                            $kw3 .= ' ';

                        }

                        if ($m == $j) {

                            $tail .= $kw3 . ' ' . $num . ' ' . $prices;

                        } else {

                            $tail .= $kw3 . "\n";

                        }

                        break;

                    } else {

                        $next_new = mb_substr($str, $m, $j, 'utf-8');

                        if (mb_strwidth($next_new, 'utf-8') < $blankNum) continue;

                        else {

                            $m = $i + 1;

                            $result[] = $new;

                            $j = 1;

                        }

                    }

                }

            }

            $head = '';

            foreach ($result as $key => $value) {

                if ($key < 1) {

                    $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);

                    $v_lenght = strlen($v_lenght);

                    if ($v_lenght == 13) $value = $value . " ";

                    $head .= $value . ' ' . $num . ' ' . $prices;

                } else {

                    $head .= $value . "\n";

                }

            }

            $orderInfo .= $head . $tail . "\n";

            @$nums += $prices;

        }

        return $orderInfo;

    }


    public function error($message)
    {

        throw new \Exception($message);

    }


}

